package com.mc.config;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class AsyncThreadPoolConfig implements AsyncConfigurer {

	@Override
	public Executor getAsyncExecutor() {
		int cpuNum = Runtime.getRuntime().availableProcessors();
		// 线程数 = cpu*2
		ThreadPoolExecutor pool = new ThreadPoolExecutor(1,
				cpuNum * 2 + 1, 60, 
				TimeUnit.SECONDS,
				new ArrayBlockingQueue<>(10), 
				Executors.defaultThreadFactory(), 
				new ThreadPoolExecutor.AbortPolicy()); 

		return pool;
	}
}
